using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Creditcardrepayment
{
	class Delegate
	{
		public delegate void mydelegate(); /*定义委托*/
		public event mydelegate NotifyMoney;/*定义事件*/


		public void Notify()
		{
			// 如果事件不为 null
			if (NotifyMoney != null)
			{
				Console.WriteLine("触发还款事件：");
				// 触发事件
				NotifyMoney();
			}
		}

	}
	class CreditCard/*定义信用卡类*/
	{
		int repaymentdata;
		private int availablet = 300000;/*信用卡可用总数*/

		private string name;/*名字*/

		int cost;/*信用卡花费*/

		public CreditCard(string name, int repaymentdata, int cost)
		{
			this.name = name;
			this.repaymentdata = repaymentdata;
			this.cost = cost;
		}
		//属性
		public string Name
		{
			get { return name; }
			set { name = value; }
		}
		public int Repaymentdata
		{
			get { return repaymentdata; }
			set { repaymentdata = value; }
		}
		public int Cost
		{
			get { return cost; }
			set { cost = value; }
		}
	}

	class CashCard/*定义储蓄卡类*/
	{
		string name;
		int remain_money;/*储蓄卡余额*/
		int today;/*时间*/
		public CashCard(string name, int remain_money, int today)/*构造*/
		{
			this.name = name;
			this.remain_money = remain_money;
			this.today = today;
		}
		//属性
		public string Name
		{
			get { return name; }
			set { name = value; }
		}
		public int Remain_money
		{
			get { return remain_money; }
			set { remain_money = value; }
		}
		public int Today
		{
			get { return today; }
			set { today = value; }
		}
	}
	//定义还款类
	class PayBackMoney
	{
		CreditCard creditcard;
		CashCard cashcard;
		public PayBackMoney(CreditCard creditcard, CashCard cashcard)/*构造*/
		{
			this.creditcard = creditcard;
			this.cashcard = cashcard;
		}
		public void PayMoney()
		{

			if (cashcard.Remain_money < creditcard.Cost)
			{
				creditcard.Cost = creditcard.Cost - cashcard.Remain_money;

				Console.WriteLine(" {0}余额不足，还款未成功，请及时还款！当前欠费{1}元，储蓄卡余额为0", creditcard.Name, creditcard.Cost);
			}
			else
			{
				cashcard.Remain_money = cashcard.Remain_money - creditcard.Cost;

				Console.WriteLine(" {0} 欠款已还清，目前储蓄卡余额为{1}元", cashcard.Name, cashcard.Remain_money);
			}

		}
	}


	class Program
	{
		[STAThread]
		static void Main(string[] args)
		{
			CreditCard a1 = new CreditCard("张三", 30, 30000);
			CashCard a = new CashCard("张三", 50000, 30);
			CreditCard b1 = new CreditCard("张四", 30, 30000);
			CashCard b = new CashCard("张四", 20000, 30);
			PayBackMoney t1 = new PayBackMoney(a1, a);
			PayBackMoney t2 = new PayBackMoney(b1, b);
			Delegate objDelegate1 = new Delegate();
			Delegate objDelegate2 = new Delegate();
			objDelegate1.NotifyMoney += new Delegate.mydelegate(t1.PayMoney);/*事件订阅对象*/
			objDelegate2.NotifyMoney += new Delegate.mydelegate(t2.PayMoney);
			if (a1.Repaymentdata == a.Today)
				objDelegate1.Notify();
			objDelegate2.Notify();
			Console.ReadLine();
		}
	}

}
